<html lang="en">
<head>
    @include('Admin.header')
</head>
<body>
<!-- 顶部开始 -->
<div style="text-align:center;margin: 20px;">{{$name}}</div>
<form class="layui-form" lay-filter="f" id="form">
    @foreach($list as $k => $v)
    <div>
        <div class="layui-form-item module_{{$v['m_id']}}">
            <label class="layui-form-label">{{$v['m_name']}}</label>
            <div class="layui-input-block">
                <input type="checkbox" class="a" names="sub[{{$k}}][m_id]" lay-skin="switch" lay-text="ON|OFF" checked lay-filter="module" value="{{$v['m_id']}}">
            </div>
        </div>
            <div style="margin-left: 110px;">
                @foreach($v['arr'] as $kk => $vv)
                <div class="layui-form-item ">
                    <input type="checkbox" class="b" names="sub[{{$k}}][arr][{{$kk}}][c_id]" title="{{$vv['c_name']}}" lay-filter="classify" value="{{$vv['c_id']}}" @if($vv['is_check'] == 1) checked @endif>
                </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                @foreach($vv['arr'] as $kkk => $vvv)
                                <input type="checkbox" class="c" name="sub[{{$k}}][arr][{{$kk}}][arr][{{$kkk}}][authority_id]" title="{{$vvv['name']}}" lay-filter="authority" value="{{$vvv['id']}}" @if($vvv['is_check'] == 1) checked @endif>
                                @endforeach
                            </div>
                        </div>
                @endforeach
            </div>
    </div>
    @endforeach
    <div class="layui-form-item">
        <div class="layui-input-block">
            {{csrf_field()}}
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
@component('Admin.section.bg')
@endcomponent
</body>
</html>
<script>
    layui.use('form', function(){
        var form = layui.form;

        $("#reset").click(function(){
            $('input[type=checkbox]').prop('checked',false);
            $(".a").click();
            form.render('checkbox');
            return false;
        });

        form.on('switch(module)', function(data){
            if(false === data.elem.checked){
                $(data.elem).parent().parent().next().find('input').attr('disabled',true);
            }else{
                $(data.elem).parent().parent().next().find('input').attr('disabled',false);
            }
            form.render('checkbox');
        });

        form.on('checkbox(classify)', function(data){
            if(false === data.elem.checked){
                $(data.elem).parent().next().find('input:checkbox').prop('checked',false);
            }else{
                $(data.elem).parent().next().find('input:checkbox').prop('checked',true);
            }
            form.render('checkbox');
        });

        form.on('checkbox(authority)', function(data){
            var s = $(data.elem).siblings('.c').length + 1;//当前分类的所有权限列表数量
            var k = $(data.elem).parent().find("input:checked").length;//当前分类已选择的权限数量
            if(0 === k){
                $(data.elem).parent().parent().prev().find('.b').prop('checked',false);//分类按钮
            }else{
                $(data.elem).parent().parent().prev().find('.b').prop('checked',true);//分类按钮
            }
            form.render('checkbox');
        });

        form.on('submit(*)', function(data){
            $.ajax({
                dataType:'json',
                type: 'post',
                url: '{{url("Admin/System/role/authority/do")}}',
                data: $('#form').serialize(),
                success: function(data){
                    if(1 == data){
                        layer.msg('操作成功!', {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            parent.layer.closeAll();
                        });
                    }else{
                        layer.msg('操作失败!');
                    }
                }
            });
            return false;
        });

    });
</script>